//
//  GlobleViewController.m
//  OneWordNew
//
//  Created by qianfeng on 15-7-2.
//  Copyright (c) 2015年 朱共营. All rights reserved.
//

#import "GlobleViewController.h"
#import "HotCell.h"
#import "NewModel.h"
#import "AFNetworking.h"
#import "FondCell.h"
#import "UIImageView+WebCache.h"
#import "FenLeiViewController.h"
#import "MyWebViewController.h"
@interface GlobleViewController ()<UIScrollViewDelegate>
{
    NSMutableArray *_dataArr;
    NSMutableArray *_myNameArr;
    UIView *_myView;
    AFHTTPRequestOperationManager *_manager;
    UICollectionView *_collectionView;
    UIScrollView *_scrollView;
    
    UIPageControl *_pageControl;
    
    NSMutableArray *_picArr;
    NSMutableArray *_nameArr;
    NSMutableArray *_urlArr;
    NSMutableArray *_idArr;
    
    

    
}
@property(nonatomic,retain) NSMutableArray *idArr;
@property(nonatomic,retain) NSMutableArray *myNameArr;
@property(nonatomic,retain) NSMutableArray *picArr;
@property(nonatomic,retain) NSMutableArray *nameArr;
@property(nonatomic,retain) NSMutableArray *urlArr;
@property(nonatomic,strong) UIScrollView *scrollView;
@property(nonatomic,retain) UICollectionView *collectionView;
@property(nonatomic,retain) NSMutableArray *dataArr;
@property(nonatomic,retain) UIView *myView;
@property(nonatomic,strong) UIPageControl *pageControl;


@end

@implementation GlobleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   // [self creatScrollView];
    [self creatUI];
    [self loadDataPage:1 isRefresh:YES];
}
- (void)creatUI{
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed: @"top"] forBarMetrics:UIBarMetricsDefault];
    self.automaticallyAdjustsScrollViewInsets=NO;
    self.dataArr=[[NSMutableArray alloc] init];
    self.myNameArr=[[NSMutableArray alloc] init];
    self.picArr=[[NSMutableArray alloc] init];
    self.nameArr=[[NSMutableArray alloc] init];
    self.urlArr=[[NSMutableArray alloc] init];
    self.idArr=[[NSMutableArray alloc ] init];
   // self.tableView=[[UIView alloc] initWithFrame:CGRectMake(0, 160, kScreenSize.width, kScreenSize.height) style:UITableViewStylePlain];
    self.myView=[[UIView alloc] initWithFrame:CGRectMake(0, 90, kScreenSize.width, kScreenSize.height)];
    _manager=[AFHTTPRequestOperationManager manager];
    _manager.responseSerializer=[AFHTTPResponseSerializer serializer];
     self.dataArr=[[NSMutableArray alloc] initWithObjects:@"loveIcon",@"characterIcon",@"abilityIcon",@"memberIcon",@"professionIcon", nil];
    self.myNameArr=[[NSMutableArray alloc] initWithObjects:@"爱情测试",@"性格测试",@"能力测试",@"会员测试",@"专业测试", nil];
    
    [self creatButton];
    [self.view addSubview:self.myView];
    
   
    

}
- (void)creatButton{
    NSInteger p,q;
    for (NSInteger i=0;i<5 ; i++) {
        if (i%2==0) {
            p=0;
        }else{
            p=1;
        }
        q=i/2;
       
            NSLog(@"%@",self.dataArr);
        UILabel *lable=[[UILabel alloc] initWithFrame:CGRectMake(q*kScreenSize.width/3+25, p*100+160, 80, 20)];
        
        lable.text=self.myNameArr[i];
        lable.font=[UIFont systemFontOfSize:14];
            UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];
            button.frame=CGRectMake(q*kScreenSize.width/3+20, p*100+80, 80, 80);
            [button setImage:[[UIImage imageNamed:self.dataArr[i] ] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        button.tag=101+i;
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.myView addSubview:lable];
            [self.myView addSubview:button];
       
    }

}
- (void)buttonClick:(UIButton *)btn{
    
    NSLog(@"%ld",btn.tag);
    
    FenLeiViewController *fenl=[[FenLeiViewController alloc] init];
    fenl.idd=(int)btn.tag-100;
    NSLog(@"%d",fenl.idd);
    fenl.titleName=self.myNameArr[fenl.idd-1];
      NSLog(@"%@",fenl.titleName);
    
    
    if (fenl.idd==4) {
        NSString *token=[[NSUserDefaults standardUserDefaults] objectForKey:@"token"];
        if (token.length>3) {
            
        }else{
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"亲！请先登录" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
            [alert show];
            return ;
        }
    }

    
    
    
    [self.navigationController pushViewController:fenl animated:YES];
    
    

}
- (void)creatScrollView{
    // NSLog(@"%@",self.dataArr);
  //  UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenSize.width, 160)];
    self.scrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenSize.width, 160)];
    
  //  NSLog(@"%@",self.imageData);
    for (NSInteger i=0; i<4; i++) {
        //  NSLog(@"%@",self.nameData[i]);
        UILabel *lable=[[UILabel alloc] init];
        lable.frame=CGRectMake(0, 130, kScreenSize.width, 30);
        lable.backgroundColor=[UIColor grayColor];
        lable.font=[UIFont systemFontOfSize:14];
        UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(kScreenSize.width*i, 0, kScreenSize.width, 160)];
      
       
       [imageView sd_setImageWithURL:[NSURL URLWithString:self.picArr[i]] placeholderImage:[UIImage imageNamed: @"loading"]];
        imageView.tag=101+i;
        lable.text=self.nameArr[i];
        imageView.userInteractionEnabled=YES;
        
        UITapGestureRecognizer *g=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(btnClick:)];
        [imageView addGestureRecognizer:g];
        
        
        
        //     NSLog(@"%@",lable.text);
        [imageView addSubview:lable];
        [self.scrollView addSubview:imageView];
        
        
        
    }
    
    
    self.scrollView.contentSize=CGSizeMake(kScreenSize.width*4, 160);
    self.scrollView.showsVerticalScrollIndicator=YES;
    self.scrollView.pagingEnabled=YES;
    self.scrollView.delegate=self;
   // [view addSubview:self.scrollView];
    
    _pageControl =[[UIPageControl alloc] initWithFrame:CGRectMake(kScreenSize.width*3/4, 160-30, kScreenSize.width/4, 30)];
    _pageControl.numberOfPages=self.picArr.count;
    
    [_pageControl addTarget:self action:@selector(pageControlClick:) forControlEvents:UIControlEventValueChanged];
    
    self.scrollView.bounces=NO;
  //  [view addSubview:_pageControl];
    
    [self.view addSubview:self.scrollView];
    [self.view addSubview:_pageControl];
   // self.tableView.tableHeaderView=view;
    
}
- (void)btnClick:(UITapGestureRecognizer *)btn{
     NSInteger index=[(UIGestureRecognizer *)btn view].tag;
    switch (index-100) {
        case 1:
        {
            MyWebViewController *web=[[MyWebViewController alloc] init];
            web.webUrl=self.urlArr[0];
            web.idd=self.idArr[0];
            [self.navigationController pushViewController:web animated:YES];
        
        }
            break;
        case 2:
        {
            MyWebViewController *web=[[MyWebViewController alloc] init];
            web.webUrl=self.urlArr[1];
            web.idd=self.idArr[1];
            [self.navigationController pushViewController:web animated:YES];
            
        }
            break;
        case 3:
        {
            MyWebViewController *web=[[MyWebViewController alloc] init];
            web.webUrl=self.urlArr[2];
             web.idd=self.idArr[2];
            [self.navigationController pushViewController:web animated:YES];
        }
            break;
        case 4:
        {
            MyWebViewController *web=[[MyWebViewController alloc] init];
            web.webUrl=self.urlArr[3];
             web.idd=self.idArr[3];
            [self.navigationController pushViewController:web animated:YES];
        }
            break;
            
        default:
            break;
    }

}
- (void)pageControlClick:(UIPageViewController *)pag{
    [self.scrollView setContentOffset:CGPointMake(self.scrollView.bounds.size.width*4, 0) animated:YES];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    CGPoint offeset=_scrollView.contentOffset;
    _pageControl.currentPage=offeset.x/kScreenSize.width;
    
}

- (void)loadDataPage:(NSInteger)page isRefresh:(BOOL)isRefresh{
    __weak typeof (self)weakSelf=self;
    NSString *url=[NSString stringWithFormat:kURLSec2];
    [_manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        if (responseObject) {
            NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            NSArray *data=dict[@"data"];
            for (NSDictionary *sub in data) {
                NewModel *model=[[NewModel alloc] init];
                [weakSelf.picArr addObject:sub[@"cover"]];
                [weakSelf.nameArr addObject:sub[@"title"]];
                [weakSelf.urlArr addObject:sub[@"absolute_url"]];
                [weakSelf.idArr addObject:sub[@"id"]];
                [model setValuesForKeysWithDictionary:sub];
                
            }
            [self creatScrollView];
            
            
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"下载失败");
        
    }];
    
    
}

    
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden=NO;
    
}/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
